package com.jiayang.business.stationpage;

import com.jiayang.base.constant.KeyConstant;
import com.jiayang.base.equipment.StaEquipmentMapper;
import com.jiayang.base.equipment.domain.dto.EquipmentFindDTO;
import com.jiayang.base.equipment.domain.entity.StaEquipment;
import com.jiayang.base.sensor.StaSensorMapper;
import com.jiayang.base.sensor.domain.entity.StaSensor;
import com.jiayang.base.station.StaStationMapper;
import com.jiayang.base.station.domain.entity.StaStation;
import com.jiayang.business.stationpage.domain.dto.EquipmentListDTO;
import com.jiayang.business.stationpage.domain.dto.EquipmentQueryDTO;
import com.jiayang.business.stationpage.domain.dto.SensorNumTypeDTO;
import com.jiayang.business.stationpage.domain.dto.StationMapDTO;
import com.jiayang.common.domain.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：OuyangChenglong
 * @ Date       ：Created in 10:11 2021/9/8
 * @ Description：场站首页服务层
 * @ Modified By：
 * @Version: $
 */
@Service
public class StationPageService {

    @Autowired
    private StaStationMapper stationDao;

    @Autowired
    private StaEquipmentMapper staEquipmentMapper;

    @Autowired
    private StaSensorMapper sensorDao;

    public ResponseDTO getStationAndEquipment(String sid) {
        // 获取场站地图
        StaStation stationEntity = stationDao.selectById(sid);
        String stationMapUrl = stationEntity.getMapUrl();
        // 获取设备坐标信息
        List<EquipmentFindDTO> equipment = staEquipmentMapper.getEquipmentByStationId(sid);
        StationMapDTO stationMapDTO = new StationMapDTO(stationMapUrl,equipment);
        return ResponseDTO.succData(stationMapDTO);
    }

    public ResponseDTO getEquipmentState(String sid) {
        // 停机设备数
        int downVal = 0;
        // 正常运行设备数
        int normalVal = 0;
        // 预警设备数
        int warningVal = 0;
        // 告警设备数
        int errorVal = 0;
        List<EquipmentFindDTO> equipmentByStationId = staEquipmentMapper.getEquipmentByStationId(sid);
        for (int i = 0; i < equipmentByStationId.size(); i++) {
            EquipmentFindDTO equipmentFindDTO = equipmentByStationId.get(i);
            String state = equipmentFindDTO.getState();
            int parseInt = Integer.parseInt(state);
            if (parseInt == 0){
                downVal++;
            }else if (parseInt == 1){
                normalVal++;
            }else if (parseInt == 2){
                warningVal++;
            }else {
                errorVal++;
            }
        }
        HashMap<String, Integer> data = new HashMap<>();
        data.put(KeyConstant.NUMBER_OF_SHUT_DOWN,downVal);
        data.put(KeyConstant.NUMBER_OF_NORMAL,normalVal);
        data.put(KeyConstant.NUMBER_OF_WARN,warningVal);
        data.put(KeyConstant.NUMBER_OF_ERROR,errorVal);
        return ResponseDTO.succData(data);
    }

    public ResponseDTO getSensorState(String sid) {
        // 正常传感器数
        int normal = 0;
        // 报警传感器数
        int error = 0;
        // 需要校准传感器数
        int needCheck = 0;
        List<StaSensor> list = sensorDao.getSensorByStationId(sid);
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++){
            StaSensor StaSensor = list.get(i);
            String state = StaSensor.getState();
            int parseInt = Integer.parseInt(state);
            if (parseInt == 0){
                error++;
            }else if (parseInt == 1){
                normal++;
            }else {
                needCheck++;
            }
        }
        map.put(KeyConstant.NUMBER_OF_SENSOR_ERROR,error);
        map.put(KeyConstant.NUMBER_OF_NORMAL,normal);
        map.put(KeyConstant.NUMBER_OF_SENSOR_NEED_CHENCK,needCheck);
        return ResponseDTO.succData(map);
    }

    public ResponseDTO getInfraredState(String sid) {
        // 停机设备数
        int downVal = 0;
        // 正常运行设备数
        int normalVal = 0;
        // 温度预警设备数
        int warningVal = 0;
        // 温度告警设备数
        int errorVal = 0;
        List<StaSensor> list = sensorDao.getSensorByStationId(sid);
        Map<String, Integer> data = new HashMap<>();
        for (int i = 0; i < list.size(); i++){
            StaSensor StaSensor = list.get(i);
            String state = StaSensor.getState();
            int stateInt = Integer.parseInt(state);
            String type = StaSensor.getType();
            int typeInt = Integer.parseInt(type);
            if (stateInt == 0 && typeInt == 2){
                downVal++;
            }else if (stateInt == 1 && typeInt == 2){
                normalVal++;
            }else if (stateInt == 3 && typeInt == 2){
                warningVal++;
            }else if (stateInt == 4 && typeInt == 2){
                errorVal++;
            }
        }
        data.put(KeyConstant.NUMBER_OF_SHUT_DOWN,downVal);
        data.put(KeyConstant.NUMBER_OF_NORMAL,normalVal);
        data.put(KeyConstant.NUMBER_OF_INFRARED_OVER_HEAT_WARN,warningVal);
        data.put(KeyConstant.NUMBER_OF_INFRARED_OVER_HEAT_ERROR,errorVal);
        return ResponseDTO.succData(data);
    }

    public ResponseDTO getOilState(String sid) {
        // 停机设备数
        int downVal = 0;
        // 正常运行设备数
        int normalVal = 0;
        // 油液预警设备数
        int warningVal = 0;
        // 油液告警设备数
        int errorVal = 0;
        List<StaSensor> list = sensorDao.getSensorByStationId(sid);
        Map<String, Integer> data = new HashMap<>();
        for (int i = 0; i < list.size(); i++){
            StaSensor StaSensor = list.get(i);
            String state = StaSensor.getState();
            int stateInt = Integer.parseInt(state);
            String type = StaSensor.getType();
            int typeInt = Integer.parseInt(type);
            if (stateInt == 0 && typeInt == 3){
                downVal++;
            }else if (stateInt == 1 && typeInt == 3){
                normalVal++;
            }else if (stateInt == 3 && typeInt == 3){
                warningVal++;
            }else if (stateInt == 4 && typeInt == 3){
                errorVal++;
            }
        }
        data.put(KeyConstant.NUMBER_OF_SHUT_DOWN,downVal);
        data.put(KeyConstant.NUMBER_OF_NORMAL,normalVal);
        data.put(KeyConstant.NUMBER_OF_OIL_WARN,warningVal);
        data.put(KeyConstant.NUMBER_OF_OIL_ERROR,errorVal);
        return ResponseDTO.succData(data);
    }

    public ResponseDTO getAllEquipment(String sid) {
        // 获取设备实体
        List<StaEquipment> equipmentEntities = staEquipmentMapper.getEquipmentEntityByStationId(sid);
        // 获取传感器数
        int count = 0;
        ArrayList<EquipmentListDTO> equipmentListDTOS = new ArrayList<>();
        for (int i = 0; i < equipmentEntities.size(); i++) {
            StaEquipment StaEquipment = equipmentEntities.get(i);
            String StaEquipmentId = StaEquipment.getId();
            // 根据设备id获取传感器类型和数量
            SensorNumTypeDTO sensorNumber = sensorDao.getSensorNumberTypeByEquipmentId(StaEquipmentId);
            EquipmentListDTO equipmentListDTO = new EquipmentListDTO(StaEquipment, sensorNumber);
            equipmentListDTOS.add(equipmentListDTO);
        }
        return ResponseDTO.succData(equipmentListDTOS);
    }

    public ResponseDTO getEquipmentByQuery(String sid,EquipmentQueryDTO equipmentQuery) {
        String name = equipmentQuery.getName();
        String state = equipmentQuery.getState();
        List<StaEquipment> equipmentEntities = staEquipmentMapper.getEquipmentByNameAndState(sid,name,state);
        ArrayList<EquipmentListDTO> equipmentListDTOS = new ArrayList<>();
        for (int i = 0; i < equipmentEntities.size(); i++) {
            StaEquipment StaEquipment = equipmentEntities.get(i);
            String StaEquipmentId = StaEquipment.getId();
            // 根据设备id获取传感器类型和数量
            SensorNumTypeDTO sensorNumber = sensorDao.getSensorNumberTypeByEquipmentId(StaEquipmentId);
            EquipmentListDTO equipmentListDTO = new EquipmentListDTO(StaEquipment, sensorNumber);
            equipmentListDTOS.add(equipmentListDTO);
        }
        return ResponseDTO.succData(equipmentListDTOS);
    }
}
